<HTML>
<HEAD>
	<TITLE></TITLE>
</HEAD>
<script type="text/javascript">

var operation = "undefined";
var operation_sign;
var operation_func;
var rank = -1;
var ans = "";
var ans_rank;
var right_ans = 0;
var right_score = 0;
var wrong_score = 0;
var randomnumber1 = 0;
var randomnumber2 = 0;
var secCounter = 60;
var isGameOver = 0;
var left_nums = [];
var right_nums = [];

function Start() {
	var op_form = document.getElementsByName("operation");
	var input_len = op_form.length;
	for (var i = 0; i < input_len; i++) {
		if (op_form[i].checked)
		{
			operation = op_form[i].value;
			break;
		}
	}
	if (operation == "undefined") return 0;
	
	op_form = document.getElementById("rank");
	rank = op_form.options[op_form.selectedIndex].value;
	
	switch (operation) {
		case "Sum":
			operation_func = newSum;
			ans_rank = rank;
			operation_sign = "+";
			break;
		case "Dif":
			operation_func = newDif;
			ans_rank = rank;
			operation_sign = "-";
			break;
		case "Multiply":
			operation_func = newMultiply;
			ans_rank = 2*rank;
			operation_sign = "*";
			break;
		case "Del":
			operation_func = newDel;
			ans_rank = rank;
			operation_sign = "/";
			break;
		case "All":
			operation_func = newAll;
			break;
	}
	document.getElementById('StartForm').style.visibility = 'hidden'; 
	document.documentElement.addEventListener("keydown", keyHandler, false);
	window.setTimeout("timer()", 1000);
	newTask(operation_func);
}

function timer() {
	if (secCounter != 0) {
		secCounter = secCounter - 1;
		window.setTimeout("timer()", 1000);
	} else {
		var d = document.getElementById("txt1");
		d.firstChild.nodeValue = "Time is out, your score is " + right_score.toString();
		isGameOver = 1;
	}
}

function isTaskUniq(number1, number2) {
	for (i = 0; i < left_nums.length - 1; i++)
		if ((number1 == left_nums[left_nums.length] &&
			number2 == right_nums[right_nums.length])||
			(number2 == left_nums[left_nums.length] &&
			number1 == right_nums[right_nums.length]) == 0) {
			return 1;
		} else {
			return 0;
		}
}

function degree(number, deg) {
	var result = number;
	if (deg == 0) return 1;
	for(var i = 1; i < deg; ++i) 
		result *= number;
	return result;
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function newSum() {	
	randomnumber1 = getRandomInt(degree(10, rank - 1), degree(10, rank)
		- degree(10, rank - 1) - 1);
	randomnumber2 = getRandomInt(degree(10, rank - 1), degree(10, rank)
		- randomnumber1 - 1);
	
	while (isTaskUniq(randomnumber1, randomnumber2) == 0) {
		randomnumber1 = degree(10, rank - 1) + 
			Math.floor(Math.random()*(degree(10, rank) - degree(10, rank - 1) - 1));
		randomnumber2 = getRandomInt(degree(10, rank - 1), degree(10, rank)
		- randomnumber1 - 1);
	}
	right_ans = randomnumber1 + randomnumber2;
}

function newDif() {
	newSum();
	var temp_randnum1 = randomnumber1;
	randomnumber1 = right_ans;
	right_ans = temp_randnum1;	
}

function newMultiply() {
	randomnumber1 = 1 + getRandomInt(degree(10, rank - 1), degree(10, rank) - 2);
	randomnumber2 = getRandomInt(Math.floor(degree(10, 2*rank - 1)/randomnumber1) + 1,
		degree(10, rank) - 1);
	while (isTaskUniq(randomnumber1, randomnumber2) == 0) {
		randomnumber1 = 1 + getRandomInt(degree(10, rank - 1), degree(10, rank) - 2);
		randomnumber2 = getRandomInt(Math.floor(degree(10, 2*rank - 1)/randomnumber1) + 1,
			degree(10, rank) - 1);
	}
	right_ans = randomnumber1*randomnumber2;
}

function newDel() {
	newMultiply();
	var temp_randnum1 = randomnumber1;
	randomnumber1 = right_ans;
	right_ans = temp_randnum1;	
}
	

function newTask(TaskFunction) {
	var d = document.getElementById("txt1");
	TaskFunction();
	d.firstChild.nodeValue = randomnumber1.toString() + 
		" " + operation_sign + " " + randomnumber2.toString() + " = ";
	left_nums[left_nums.length] = randomnumber1;
	right_nums[right_nums.length] = randomnumber2;
}

function keyHandler(event) {
  event.preventDefault(); 
  if (isGameOver == 1) return;
  var d = document.getElementById("txt1");
  if (ans.length < ans_rank - 1) {
	ans = ans + String.fromCharCode(event.keyCode);
	d.firstChild.nodeValue += String.fromCharCode(event.keyCode);
	} 
	else if (ans.length == ans_rank - 1) {
		ans = ans + String.fromCharCode(event.keyCode);
		if (ans == right_ans.toString()) {
			ans = "";
			right_score++;
			newTask(operation_func);
		} else {
			ans = "";
			wrong_score++;
			newTask(operation_func);
		}
	}
  //d.firstChild.nodeValue = ans;
}
</script>
 <BODY>	
	<p id = "txt1" style="font-size: 26pt">Welcome to brain train!</p>
	<div id="StartForm" style="font-size: 26pt">
	Choose your operation:<br>
	
	<input type="radio" name="operation" value="Sum" checked="true"> +<br>
	<input type="radio" name="operation" value="Dif"> -<br>
	<input type="radio" name="operation" value="Multiply"> *<br>
	<input type="radio" name="operation" value="Del"> /<br>
	<input type="radio" name="operation" value="All"> All the operation<br><br>
	Choose number rank:	<br><br>
	<select id = "rank" name = "rank_form" size="4">
		<option>	1
		<option selected = "true">	2
		<option>	3
		<option>	4
		<option>	5
		<option>	6
	</select><br><br>
	<input type="button" onclick="Start()" value="Start >>"><br><br>
	</div>
</BODY>
</HTML>